/*
 *  Cero Project - Copyright   2006 The Cero Developement Team 
 *  (Michael Laguerre, Camille Roux, Matthieu Segret, Mathieu Sivade) 
 * 
 *  This program is free software; you can redistribute it and/or modify it 
 *  under the terms of the GNU General Public License as published by the Free 
 *  Software Foundation; either version 2 of the License, or (at your option) 
 *  any later version.
 * 
 *  This program is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 *  for more details.
 */

package org.ceroproject;

import java.util.HashMap;
import java.util.List;

/**
 * Configurer class. This class is used to save Integer, String or List object.
 * Each object is associated to a String (its name).
 * 
 * @author Roux Camille
 * 
 */
public class Configurer {
	private HashMap<String, Integer> intMap;

	private HashMap<String, String> stringMap;

	private HashMap<String, List<String>> listMap;

	private String id;

	/**
	 * Create a Configurer object
	 * 
	 * @param id
	 *            id of this configurer
	 */
	public Configurer(String id) {
		intMap = new HashMap<String, Integer>();
		stringMap = new HashMap<String, String>();
		listMap = new HashMap<String, List<String>>();
		this.id = id;
	}

	/**
	 * Returns the id of the Configurer
	 * 
	 * @return the id of the Configurer
	 */
	public String getId() {
		return id;
	}

	/**
	 * Returns the int linked to a property
	 * 
	 * @param propertyName
	 *            the Name of the property
	 * @return Returns the int linked to the property
	 */
	public int getInt(String propertyName) {
		return intMap.get(propertyName);
	}

	/**
	 * Returns the String linked to a property
	 * 
	 * @param propertyName
	 *            the Name of the property
	 * @return Returns the String linked to the property
	 */
	public String getString(String propertyName) {
		return stringMap.get(propertyName);
	}

	/**
	 * Returns the List linked to a property
	 * 
	 * @param propertyName
	 *            the Name of the property
	 * @return Returns the List linked to the property
	 */
	public List<String> getList(String propertyName) {
		return listMap.get(propertyName);
	}

	/**
	 * Save an int value linked to a property name
	 * 
	 * @param propertyName property name
	 * @param value the value to save
	 * @throws ConfigurationException the exception explaining why it's invalid
	 */
	public void setInt(String propertyName, int value)
			throws ConfigurationException {
		validateIntValue(propertyName, value);
		// we won't get here if an exception got thrown
		intMap.put(propertyName, value);
	}

	/**
	 * Save an String value linked to a property name
	 * 
	 * @param propertyName property name
	 * @param value the value to save
	 * @throws ConfigurationException the exception explaining why it's invalid
	 */
	public void setString(String propertyName, String value)
			throws ConfigurationException {
		stringMap.put(propertyName, value);
	}

	/**
	 * Save an List value linked to a property name
	 * 
	 * @param propertyName property name
	 * @param value the value to save
	 * @throws ConfigurationException the exception explaining why it's invalid
	 */
	public void setList(String propertyName, List<String> value)
			throws ConfigurationException {
		listMap.put(propertyName, value);
	}

	/**
	 * Throws an exception if the value that should have been set is invalid
	 * 
	 * @param propertyName
	 *            the name of the property to set
	 * @param value
	 *            the value to set the property to
	 * @throws ConfigurationException
	 *             the exception explaining why it's invalid
	 */
	public void validateIntValue(String propertyName, int value)
			throws ConfigurationException {
		if (propertyName == null || propertyName.equals(""))
			throw new ConfigurationException(
					"There is no empty property allowed by " + id);
	}

	/**
	 * Throws an exception if the value that should have been set is invalid
	 * 
	 * @param propertyName
	 *            the name of the property to set
	 * @param value
	 *            the value to set the property to
	 * @throws ConfigurationException
	 *             the exception explaining why it's invalid
	 */
	public void validateStringValue(String propertyName, String value)
			throws ConfigurationException {
		if (propertyName == null || propertyName.equals(""))
			throw new ConfigurationException(
					"There is no empty property allowed by " + id);
	}

	/**
	 * Throws an exception if the value that should have been set is invalid
	 * 
	 * @param propertyName
	 *            the name of the property to set
	 * @param value
	 *            the value to set the property to
	 * @throws ConfigurationException
	 *             the exception explaining why it's invalid
	 */
	public void validateListValue(String propertyName, List<String> value)
			throws ConfigurationException {
		if (propertyName == null || propertyName.equals(""))
			throw new ConfigurationException(
					"There is no empty property allowed by " + id);
	}

	/**
	 * Returns the map of properties with integer values
	 * 
	 * @return the map of properties with integer values
	 */
	public HashMap<String, Integer> getIntProperties() {
		return intMap;
	}

	/**
	 * Returns the map of properties with String values
	 * 
	 * @return the map of properties with String values
	 */
	public HashMap<String, String> getStringProperties() {
		return stringMap;
	}

	/**
	 * Returns the map of properties with list values
	 * 
	 * @return the map of properties with list values
	 */

	public HashMap<String, List<String>> getListProperties() {
		return listMap;
	}

}
